/* Copyright (c) 2008 Google Inc.
*  Copyright (c) 2009 Pierre Henri Kuate.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace org.opensocial.data
{
    public class Response<T> : OpenSocialObject, System.Collections.Generic.IList<T>
        where T : OpenSocialObject
    {
        private System.Collections.Generic.IList<T> List;

        public Response()
        {
            setList(new System.Collections.Generic.List<T>());
        }

        public void setList(System.Collections.Generic.IList<T> l)
        {
            List = l;
        }


        public int? itemsPerPage
        {
            get { return getIntegerValue("itemsPerPage"); }
        }

        public long? startIndex
        {
            get { return getLongValue("startIndex"); }
        }

        public long? totalResults
        {
            get { return getLongValue("totalResults"); }
        }

        public bool? isFiltered
        {
            get { return getBoolValue("isFiltered"); }
        }

        public bool? isSorted
        {
            get { return getBoolValue("isSorted"); }
        }

        public bool? isUpdatedSince
        {
            get { return getBoolValue("isUpdatedSince"); }
        }


        #region Implementation of IEnumerable

        public System.Collections.Generic.IEnumerator<T> GetEnumerator()
        {
            return List.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion

        #region Implementation of ICollection<T>

        public void Add(T item)
        {
            List.Add(item);
        }

        public void Clear()
        {
            List.Clear();
        }

        public bool Contains(T item)
        {
            return List.Contains(item);
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            List.CopyTo(array, arrayIndex);
        }

        public bool Remove(T item)
        {
            return List.Remove(item);
        }

        public int Count
        {
            get { return List.Count; }
        }

        public bool IsReadOnly
        {
            get { return List.IsReadOnly; }
        }

        #endregion

        #region Implementation of IList<T>

        public int IndexOf(T item)
        {
            return List.IndexOf(item);
        }

        public void Insert(int index, T item)
        {
            List.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            List.RemoveAt(index);
        }

        public T this[int index]
        {
            get { return List[index]; }
            set { List[index] = value; }
        }

        #endregion


        public override string ToString()
        {
            var sb = new System.Text.StringBuilder();
            sb.AppendLine("OpenSocial Response: " + Count + " " + typeof(T).Name + "(s), startIndex=" + startIndex + ", totalResults=" + totalResults);

            foreach (var e in this)
                sb.AppendLine("- " + e);
            return sb.ToString();
        }
    }
}